package com.project.erp.model.system;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.google.common.collect.Lists;
import com.project.erp.common.utils.reflection.ConvertUtils;
import com.project.erp.model.IdEntity;

/**
 * 用户.
 * 
 * 使用JPA annotation定义ORM关系.
 * 使用Hibernate annotation定义JPA 1.0未覆盖的部分.
 * 
 * @author calvin
 */
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "TBL_USER")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends IdEntity{

	private static final long serialVersionUID = -1318191052279176462L;
	
	private String loginName;	//用户名
	private String password;	//密码
	private String realName;	//真实姓名
	private String email;		//邮箱
	private String status;		//状态  1、正常 2、锁定
	private Date createTime;	//创建时间
	
	private Date createStartTime;	//创建开始时间--用于查询
	private Date createEndTime;		//创建结束时间--用于查询
	
	private String randCheckCode;//检验码
	
	private String description;	//描述
	
	//用户关联角色信息
	private List<Role> roleList = Lists.newArrayList(); //有序的关联对象集合
	//用户关联部门主管
	private List<Department> deptSupervisors = Lists.newArrayList(); //有序的关联对象集合
	//用户关联部门经理
	private List<Department> deptManagers = Lists.newArrayList(); //有序的关联对象集合

	public User() {}
	
	public User(String loginName, String password, String realName, String email) {
		this.loginName = loginName;
		this.password = password;
		this.realName = realName;
		this.email = email;
	}

	//字段非空且唯一, 用于提醒Entity使用者及生成DDL.
	@Column(nullable = false, unique = true)
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	@JSON(format="yyyy-MM-dd HH:mm:ss")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	//多对多定义
	@ManyToMany(fetch=FetchType.LAZY)
	//中间表定义,表名采用默认命名规则
	@JoinTable(name = "TBL_USER_ROLE", joinColumns = {@JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
	//Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	//集合按id排序
	@OrderBy("id DESC")
	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	@Transient
	public String getRoleNames() {
		return ConvertUtils.convertElementPropertyToString(roleList, "name", ", ");
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	
	@Transient
	@JSON(format="yyyy-MM-dd HH:mm:ss")
	public Date getCreateStartTime() {
		return createStartTime;
	}

	public void setCreateStartTime(Date createStartTime) {
		this.createStartTime = createStartTime;
	}

	@Transient
	@JSON(format="yyyy-MM-dd HH:mm:ss")
	public Date getCreateEndTime() {
		return createEndTime;
	}

	public void setCreateEndTime(Date createEndTime) {
		this.createEndTime = createEndTime;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Transient
	public String getRandCheckCode() {
		return randCheckCode;
	}

	public void setRandCheckCode(String randCheckCode) {
		this.randCheckCode = randCheckCode;
	}

	@ManyToMany(mappedBy="supervisors",cascade=CascadeType.REFRESH) 
	public List<Department> getDeptSupervisors() {
		return deptSupervisors;
	}

	public void setDeptSupervisors(List<Department> deptSupervisors) {
		this.deptSupervisors = deptSupervisors;
	}

	@ManyToMany(mappedBy="managers",cascade=CascadeType.REFRESH) 
	public List<Department> getDeptManagers() {
		return deptManagers;
	}

	public void setDeptManagers(List<Department> deptManagers) {
		this.deptManagers = deptManagers;
	}
}